//
//  KKComposeViewController.swift
//  KKWeibo
//
//  Created by kassia on 16/7/20.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SVProgressHUD
@available(iOS 9.0, *)
class KKComposeViewController: UIViewController {

    
    // 懒加载titleLable
    private lazy var titleLable: UILabel = {
    
        let title = UILabel(textColor: UIColor.grayColor(), fontSize: 15)
        title.numberOfLines = 0
        title.textAlignment = .Center
       
        // 获取姓名
        if let name = KKUserAccountModel.sharedUserAccount.userAccount?.name {
        
           let lable = "发微博\n\(name)"
            
           let attributeStr = NSMutableAttributedString(string: lable)
            
            // 获取名字的范围
            let range = (lable as NSString).rangeOfString(name)
            
            attributeStr.addAttributes([NSForegroundColorAttributeName: UIColor.lightGrayColor(), NSFontAttributeName: UIFont.systemFontOfSize(11)], range: range)
            
            title.attributedText = attributeStr

        } else {
        
           title.text = "发微博"
        }
    
          title.sizeToFit() // 自动适配大小
          return title
    }()
    
    // 发送
    private lazy var sendButton: UIButton = {
    
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_orange"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Disabled)
        
        button.setTitle("发送", forState: .Normal)
        
        button.addTarget(self, action: "sendAction", forControlEvents: .TouchUpInside)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Disabled)
        button.size = CGSize(width: 45, height: 30)
        
        return button
    }()
    
    
    // textfiled
    private lazy var textView: KKComposeTextView = {
    
        let view = KKComposeTextView()
        view.placeHodel = "爱上对方过后就哭了"
        view.font = UIFont.systemFontOfSize(15)
        view.alwaysBounceHorizontal = true
        
        return view
    }()
    
    
    // toolbar
    private lazy var toolBar: KKComposeToolBar = {
    
        let toolBar = KKComposeToolBar(frame: CGRectZero)
//        toolBar.selectedComposeToolBarClosuer
        return toolBar
        
    }()
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    
    private func setupUI() {
         setupNavUI()
        view.addSubview(textView)
          view.addSubview(toolBar)
      
        // 添加约束
        textView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view)
            make.leading.equalTo(view)
            make.trailing.equalTo(view)
            make.bottom.equalTo(self.toolBar.snp_top)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view)
            make.leading.equalTo(view)
            make.trailing.equalTo(view)
            make.height.equalTo(30)
        }
    
    }
    private func setupNavUI() {
    
       navigationItem.leftBarButtonItem = UIBarButtonItem(target: self, title: "取消", action: "cancelAction")
       navigationItem.titleView = titleLable
    
       navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        
    }

    
    @objc private func sendAction() {
        
        // 参数
        let access_token = KKUserAccountModel.sharedUserAccount.accessToken!
        let text = textView.text!
        
        // 网络请求回来的数据
        KKNetworkTool.sharedTools.update(access_token , status: text) { (response, error) -> () in
            if error != nil {
                
                print(error)
                SVProgressHUD.showErrorWithStatus("发送失败")
                return
                
            } else {
                
                SVProgressHUD.showSuccessWithStatus("发送成功")
                
            }
        }
    
    }
    
    
    @objc private func cancelAction() {
     
        self.view.endEditing(true)
     // 取消按钮
     dismissViewControllerAnimated(true, completion: nil)
    
    }

}
